package com.dy.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.VO.UserVO;
import com.dy.exception.BusinessException;
import com.dy.response.Result;
import com.dy.response.ResultCode;
import com.dy.entity.User;
import com.dy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-03-02
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
@Api(value = "系统用户模块", tags = "系统用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUserById/{id}")
    @ApiOperation(value = "查询单个用户", notes = "通过ID查询对应的用户信息")
    public Result getUserById(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        if (user != null) {
            return Result.ok().data("user", user);
        } else {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(), ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
    }

    @PostMapping("/getUsers")
    @ApiOperation(value = "分页查询")
    public Result getUsers(@RequestParam(value="current",defaultValue="1",required = true) Integer current,
                           @RequestParam(value="size",defaultValue="7",required = true) Integer size,
                           @RequestBody(required = false) UserVO userVO) {
        Page<User> page = new Page<>(current,size);
        page = userService.findUserList(page,userVO);
        return Result.ok().data("pageList", page);
    }

    @PostMapping("/updateUserStatus")
    @ApiOperation(value = "修改用户状态")
    public Result updateUserStatus(@RequestParam(value="id",required = true) Integer id,
                           @RequestParam(value="status",required = true) Integer status) {
        User user = new User();
        user.setStatus(status);
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        boolean update = userService.update(user, updateWrapper);
        if (update){
            return Result.ok().message("修改状态成功");
        }
        return Result.error().message("修改状态失败");
    }

    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportUserExcel(HttpServletResponse response, @RequestBody(required = false) UserVO userVO) throws IOException {
        Long count = userService.getBaseMapper().selectCount(null);
        Page<User> page = new Page<>(1,count);
        Page<User> pageList = userService.findUserList(page,userVO);
        List<User> users = pageList.getRecords();
        userService.exportUserExcel(response,users);
    }

    @ApiOperation(value = "用户登录")
    @PostMapping(value = "/login")
    public Result login(@RequestParam(value="username",required = true) String username,
                        @RequestParam(value="password",required = true) String password,
                        HttpServletRequest request){
        String token = userService.login(username, password);
        return Result.ok().data("token",token);
    }



}

